/** ***********************************************
 * CodeGen
 *
 * @author  Jonnie Spratley
 * @date 3/05/2009
 * @licence MCIYC
 * @website http://jonniespratley.com/code
 * @contact jonniespratley@gmail.com
 *********************************************** */
package com.jonniespratley.codegen.business
{
	import com.adobe.cairngorm.business.ServiceLocator;
	import com.jonniespratley.codegen.vo.ConfigVO;
	import com.jonniespratley.codegen.vo.FileVO;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;

	/**
	 * The Business Delegate delegates all the responsibility for the
	 * business logic that must locate a service, and then invokes
	 * a method on the service.
	 *
	 */
	public class CodeGenDelegate
	{
		private var responder:IResponder;
		private var service:Object;

		public function CodeGenDelegate( responder:IResponder )
		{
			this.service = ServiceLocator.getInstance().getHTTPService( "CodeGenService" );
			this.responder = responder;
		}

		/************** ServiceCalls ********************/

		public function getDatabases( aHost:String, aUser:String, aPass:String ):void
		{
			var token:AsyncToken = service.send( {
												m: 'getDatabases',
												h: aHost,
												u: aUser,
												p: aPass
												} );
				token.addResponder( responder );
		}

		public function getSchema( aConfig:ConfigVO ):void
		{
			var token:AsyncToken = service.send( {
												m: 'getSchema',
												d: aConfig.aDatabase
												} );
				token.addResponder( responder );
		}

		public function getConfig( aConfig:ConfigVO ):void
		{
			var token:AsyncToken = service.send( {
												m: 'getConfig',
												d: aConfig.aDatabase
												} );
				token.addResponder( responder );

		}

		public function generateSchema( aConfig:ConfigVO ):void
		{
			var token:AsyncToken = service.send( {
											 	m: 'generateSchema',
												d: aConfig.aDatabase
											 	} );
				token.addResponder( responder );
				token.config = aConfig;
		}

		public function generateApplication( aConfig:ConfigVO ):void
		{
			var token:AsyncToken = service.send( {
												m: 'generateApplication',
												d: aConfig.aDatabase
												} );
				token.addResponder( responder );
				token.config = aConfig;
		}

		/*
		 $_GET[ 'h' ], //Host
				 $_GET[ 'u' ], //User
				 $_GET[ 'p' ], //Pass
				 $_GET[ 'd' ], //Database
				 $_GET[ 'a' ], //Application
				 $_GET[ 'e' ], //Endpoint
				 $_GET[ 'n' ], //Namespace
				 $_GET[ 'f' ], //Framework
				 $_GET[ 'c' ] //Copywrite
		*/
		public function generateConfig( aConfig:ConfigVO ):void
		{
			var token:AsyncToken = service.send( {
												m: 'generateServerSide',
												options: new Array(aConfig)

												} );
				token.addResponder( responder );
				token.config = aConfig;
		}

		public function getTemplates():void
		{
			var token:AsyncToken = service.send( {
												m: 'getTemplates'
												} );
				token.addResponder( responder );
		}

		public function getOutput():void
		{
			var token:AsyncToken = service.send( {
												m: 'getOutput'
												} );
				token.addResponder( responder );
		}

		public function getServices( aLocation:String ):void
		{
			var token:AsyncToken = service.send( {
												m: 'getServices',
												p: aLocation
												} );
				token.addResponder( responder );
		}


		public function getDirectory( aPath:String ):void
		{
			var token:AsyncToken = service.send( {
												m: 'getDirectory',
												p: aPath
												} );
				token.addResponder( responder );

		}

		public function readFile( aFile:FileVO ):void
		{
			var token:AsyncToken = service.send( {
												m: 'readFile',
												f: aFile.aPath
												} );
				token.addResponder( responder );
				token.aFile = aFile;
		}

		public function inspectClass( aFile:FileVO ):void
		{
			var token:AsyncToken = service.send( {
												m: 'inspectClass',
												f: aFile.aPath
												} );
				token.addResponder( responder );
				token.aFile = aFile;

				trace( aFile.aPath );
		}

		public function callClassFunction( classPath:String, classFunction:String, classArgs:Array ):void
		{
			var token:AsyncToken = service.send( {
												m: 'callClassFunction',
												_class: classPath,
												_func: classFunction,
												_args: classArgs.toString()
												} );
				token.addResponder( responder );


		}

		public function writeFile( aFile:FileVO, aContents:String ):void
		{
			var token:AsyncToken = service.send( {
												m: 'writeFile',
												f: aFile.aPath,
												c: aContents
												} );
				token.addResponder( responder );
				token.aFile = aFile;
		}
	}
}
